from utils.BaseModels import BaseModel
from django.db import models
from DjangoUeditor.models import UEditorField
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFill


# 商品类别库
class GoodCategory(BaseModel.CreateAndUpdateAtBaseModel):
    name = models.CharField('商品类别', max_length=32, db_index=True, unique=True)
    p_category = models.ForeignKey('self', verbose_name='父级商品类别', on_delete=models.CASCADE, null=True, blank=True,
                                   related_name='parent_good_category')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'good_category'
        verbose_name = '商品类别'
        verbose_name_plural = '商品类别管理'
        permissions = {
        }


# 单位库
class Company(BaseModel.CreateAndUpdateAtBaseModel):
    name = models.CharField('单位名字', max_length=12, db_index=True, unique=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'company'
        verbose_name = '单位'
        verbose_name_plural = '单位管理'


# 商品库
class Good(BaseModel.CreateAndUpdateAtBaseModel):
    name = models.CharField('商品名', max_length=32, db_index=True, unique=True)
    price = models.DecimalField('单价', default=0.00, max_digits=12, decimal_places=2)
    discount = models.DecimalField('折扣', default=1.00, max_digits=3, decimal_places=2)
    isDiscount = models.IntegerField('是否打折', default=1, choices=((1, '是'), (0, '否'),))
    cost = models.DecimalField('成本价', default=0.00, max_digits=12, decimal_places=2)
    integral = models.IntegerField('积分售价', default=0)
    detail = UEditorField(verbose_name=u"内容", imagePath="ueditor/images/%(basename)s_%(rnd)s_%(datetime)s.%(extname)s",
                          width=1000, height=300,
                          filePath="ueditor/files/%(basename)s_%(rnd)s_%(datetime)s.%(extname)s", default='',
                          toolbars='full', null=True, blank=True)
    goods_cover_image = models.ImageField(upload_to="goods/cover/images/", null=True, blank=True, verbose_name="封面图")
    sold_num = models.IntegerField(default=0, verbose_name="商品销售量")
    good_category = models.ForeignKey(GoodCategory, verbose_name='商品类别', on_delete=models.CASCADE,
                                      related_name='goods', null=True, blank=True)
    # company = models.ForeignKey(Company, on_delete=models.CASCADE, verbose_name='是否可以预售')
    # is_print_show = models.BooleanField(verbose_name='是否打印显示')

    # status = models.IntegerField('上架状态', default=1, choices=((0, '下架'), (1, '上架')))

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'goods'
        verbose_name = '商品'
        verbose_name_plural = '商品管理'
        permissions = {
        }


# 商品额外统计
class GoodExtraStatistics(models.Model):
    good = models.ForeignKey(Good, on_delete=models.CASCADE)


# 仓库信息
class WarehouseInformation(BaseModel.CreateAndUpdateAtBaseModel):
    head_name = models.CharField(verbose_name='负责人姓名', max_length=20)


# 仓库表
class Warehouse(BaseModel.CreateAndUpdateAtBaseModel):
    good = models.ManyToManyField(Good, related_name='goods')


# 商品轮播图
class GoodImage(BaseModel.CreateAndUpdateAtBaseModel):
    good = models.ForeignKey(Good, on_delete=models.CASCADE, verbose_name="商品", related_name="images")
    image = models.ImageField(upload_to="goods/images/%Y/%m/%d", verbose_name="图片", null=True, blank=True)
    image_thumbnail = ImageSpecField(source='image',
                                     processors=[ResizeToFill(320, 200)],
                                     format='PNG',
                                     options={'quality': 60}
                                     )

    class Meta:
        db_table = 'good_images'
        verbose_name = '商品轮播'
        verbose_name_plural = verbose_name

    @property
    def image_tag(self):
        return u'<img src="%s" />' % (self.image_thumbnail.url)

    image_tag.fget.short_description = '图片XXXXXXX'
    image_tag.fget.allow_tags = True

    def __str__(self):
        return self.good.name
